package com.yzc.mihuyou.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yzc.mihuyou.entity.Comment;
import com.yzc.mihuyou.entity.PageWuer;
import com.yzc.mihuyou.entity.User;
import com.yzc.mihuyou.mapper.CommentMapper;
import com.yzc.mihuyou.service.CommentService;
import com.yzc.mihuyou.service.PingiveService;
import com.yzc.mihuyou.service.UserService;
import com.yzc.mihuyou.service.UserimgService;
import com.yzc.mihuyou.util.TokenProccessor;
import com.yzc.mihuyou.vo.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 评论表 前端控制器
 * </p>
 *
 * @author 易紫诚
 * @since 2022-04-28
 */
@RestController
@RequestMapping("/mihuyou/comment")
//@UserLoginToken
//@PassToken
public class CommentController {

    @Autowired
    CommentService commentService;

    @Autowired
    UserService userService;

    @Autowired
    TokenProccessor tokenProccessor;

    @Autowired
    CommentMapper commentMapper;

    @Autowired
    PingiveService pingiveService;



    @PostMapping("pinRue/{id}")
    public JsonResult runRue(@PathVariable("id") int id, Integer type, PageWuer pageWuer,Integer userId, HttpServletRequest request){
        Long l=0L;
        String token = request.getHeader("token");
        User user=null;
        if(token!=null){
            user= tokenProccessor.verify(token);
        }
        if(user!=null){
            l=user.getUserId();
        }
        if(type==null){
            type=1;
        }
       if(pageWuer.getSize()==null){
           pageWuer.setSize(0L);
       }
       if(pageWuer.getCurrent()==null){
           pageWuer.setCurrent(10L);
       }
        Map<String,Object> map=commentService.selCome(id,pageWuer,type,l,userId);
        return new JsonResult(200,"查询成功",map);
    }

    @GetMapping("getComment/{id}")
    public JsonResult getComment(@PathVariable("id") long id,HttpServletRequest request){
        List listwe=new ArrayList();
        List<Comment> comment = commentService.getComment(id);
        for (Comment comment1:comment){
            Map<String,Object> map = new HashMap<>();
            comment1.setUser((User) userService.getUser(comment1.getUserId()).getData().get("data"));
            QueryWrapper<Comment> commentQueryWrapper=new QueryWrapper<>();
            commentQueryWrapper.eq("comment_id",comment1.getRootParentId());
            Comment list=commentMapper.selectOne(commentQueryWrapper);
            System.out.println(list);
            if(list.getUserId().equals(id)){
                list.setUser(userService.cuw(list.getUserId()));
                list.setTrie(pingiveService.inbut(list.getCommentId(),id));
                QueryWrapper<Comment> queryWrapper1=new QueryWrapper<>();
                queryWrapper1.eq("root_parent_id",list.getCommentId());
                queryWrapper1.eq("giv_we",0);
                queryWrapper1.orderByDesc("give_num");
                list.setChild(commentMapper.selectList(queryWrapper1));
                for(int i=0;i<list.getChild().size();i++){
                    list.getChild().get(i).setUser(userService.cuw(list.getChild().get(i).getUserId()));
                    list.getChild().get(i).setTrie(pingiveService.inbut(list.getChild().get(i).getCommentId(),id));
                    if(!list.getChild().get(i).getParentId().equals(list.getChild().get(i).getRootParentId())){
                        list.getChild().get(i).setUwer2(userService.getById(commentMapper.selectById(list.getChild().get(i).getCommentId())));
                    }
                }
                map.put("data",comment1);
                map.put("aw",list);
                listwe.add(listwe.size(),map);
            }
            }

        Map<String,Object> map=new HashMap<>();
        map.put("dawt",listwe);
        return new JsonResult(200,"查询成功",map);
    }


    @PostMapping("addCome")
    public JsonResult addCome(@RequestBody Comment comment){
        System.out.println(comment);
        if(comment.getContent()==null){
            return  new JsonResult(401,"你他喵不传值呢");
        }
        int o=commentService.addCome(comment);
        if(o>0){
            Comment comment1=commentService.serCwu(o);
            comment1.setUser(userService.cuw(comment1.getUserId()));
            comment1.setTrie(pingiveService.inbut(comment1.getCommentId(),comment.getUserId()));
            QueryWrapper<Comment> queryWrapper1=new QueryWrapper<>();
            queryWrapper1.eq("root_parent_id",comment1.getCommentId());
            queryWrapper1.eq("giv_we",0);
            queryWrapper1.orderByDesc("give_num");
            comment1.setChild(commentMapper.selectList(queryWrapper1));
            for(int i=0;i<comment1.getChild().size();i++){
                comment1.getChild().get(i).setUser(userService.cuw(comment1.getChild().get(i).getUserId()));
                comment1.getChild().get(i).setTrie(pingiveService.inbut(comment1.getChild().get(i).getCommentId(),comment.getUserId()));
                if(!comment1.getChild().get(i).getParentId().equals(comment1.getChild().get(i).getRootParentId())){
                    comment1.getChild().get(i).setUwer2(userService.getById(commentMapper.selectById(comment1.getChild().get(i).getCommentId())));
                }
            }
            Map<String,Object>map=new HashMap<>();
            map.put("com",comment1);
            return new JsonResult(200,"添加成功",map);
        }
        return new JsonResult(201,"添加失败");
    }


    @DeleteMapping("dleect")
    public JsonResult dleect(Integer id){
        if(id==null){
            return new JsonResult(401,"参数异常");
        }
        int i=commentService.delect(id);
        if(i>0){
            return new JsonResult(200,"删除成功");
        }else {
            return new JsonResult(201,"删除失败");
        }
    }



    @GetMapping("selTuw/{id}")
    public JsonResult selTuw(@PathVariable("id") Integer id,HttpServletRequest httpServletRequest){

        Long l=0L;
        String token = httpServletRequest.getHeader("token");
        User user=null;
        if(token!=null){
            user= tokenProccessor.verify(token);
        }
        if(user!=null){
            l=user.getUserId();
        }

        Map<String,Object> map=new HashMap<>();


        Comment comment=commentMapper.selectById(id);

        if(comment.getRootParentId()!=0){
            comment=commentMapper.selectById(comment.getRootParentId());
        }
            comment.setUser(userService.cuw(comment.getUserId()));
            comment.setTrie(pingiveService.inbut(comment.getCommentId(),l));

            QueryWrapper<Comment> queryWrapper1=new QueryWrapper<>();
            queryWrapper1.eq("root_parent_id",comment.getCommentId());
            queryWrapper1.eq("giv_we",0);
            queryWrapper1.orderByDesc("give_num");
            comment.setChild(commentMapper.selectList(queryWrapper1));
            for(int i=0;i<comment.getChild().size();i++){
                comment.getChild().get(i).setUser(userService.cuw(comment.getChild().get(i).getUserId()));
                comment.getChild().get(i).setTrie(pingiveService.inbut(comment.getChild().get(i).getCommentId(),l));
                if(!comment.getChild().get(i).getParentId().equals(comment.getChild().get(i).getRootParentId())){
                    comment.getChild().get(i).setUwer2(userService.getById(commentMapper.selectById(comment.getChild().get(i).getCommentId())));
                }
            }


        map.put("ListPin",comment);

        return new JsonResult(200,"查询成功",map);
    }

}

